<?php
/**
 * Bet class
 *
 * @category   Bet
 * @package    Lib
 * @copyright  Copyright (c) 2010-2020  Mr.Khuyen(http://herofit.net)
 */
class Bet{
	//http://www.flashscore.com/x/feed/d_od_8QbkWRd8_en_1_eu
	var $url;
	var $datas;
	var $continue;
	var $log;
	/*
	 * contruction init get data
	 * */
	function __construct($url){
		// url
		$this->url = $url;
		// init obj log
		$this->log = new CLogData();
		try {
			//$this->datas = file_get_contents($url);
			$curl = curl_init($url);
			curl_setopt($curl, CURLOPT_REFERER, 'http://www.flashscore.com');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl, CURLOPT_POST, true);
			$this->datas  = curl_exec($curl);
			curl_close($curl);
			
			$this->continue = true;
		}catch (Exception $e){
			$this->log->addlog('Caught exception: '.$e->getMessage());
			$this->continue = false;
		}
	}
	/*
	 * get odds bet rate
	 * 
	 * Array
	 * 	(
	 * 	    [odds] => Array
	 * 	        (
	 * 	            [home] => 3.00
	 * 	            [draw] => 3.20
	 * 	            [away] => 2.40
	 * 	        )
	 * 	
	 * 	    [exchange] => Array
	 * 	        (
	 * 	            [0] => BettingExchange1X2
	 * 	            [1] => Betfair:back:3.15:3.35:2.60:
	 * 	            [2] => lay:3.25:3.40:2.62:
	 * 	            [3] => 
	 * 	        )
	 * 	
	 * 	)
	 * 
	 * 
	 * 
	 * */
	function getBetODDS($find_bet='bet365'){
		if (empty($this->datas)) return array();
		$return = array();
		//backup
		$datas = $this->datas;
		preg_match_all("/<div id=\"block-1x2-ft\" style=\"display: block;\">(.*)<div id=\"block-1x2-1hf\" style=\"display: none;\">/U", $datas, $out);
		$content = strip_tags($out[1][0],'<table><tr><td>');
		$content = str_replace('<table id="odds_1x2_bookmaker" class="odds sortable" cellspacing="0">', '', $content);
		$content = str_replace('<table id="odds_1x2_exchange" class="odds" cellspacing="0">', '', $content);
		$content = str_replace('</table>', '--', $content);
		$content = str_replace('</td>', ':', $content);
		$content = strip_tags($content,'<tr>');
		$content = str_replace('&nbsp;', ' ', $content);
		$content = str_replace('<tr class="odd">', '', $content);
		$content = str_replace('<tr class="even">', '', $content);
		$content = str_replace('</tr>', ';', $content);
		$content_split = explode('--', $content);
		//	echo "<pre>";
		//	print_r($content_split);
		// get bet
		$bets = explode(';', $content_split[0]);
		foreach ($bets as $bet){
			if(strpos($bet,$find_bet) !== false){
				//bet365:0.5:1.06:10.00:
				$odds = explode(':', $bet);
				//echo $bet;
				$return['odds'] = array(
						'home'=>$odds[1],
						'draw'=>$odds[2],
						'away'=>$odds[3]
				);
	
				break;
			}
		}
		// get Exchange
		//$content_split[1] = preg_replace('/\(.*\)/', '', $content_split[1]);
		//echo $content_split[1];
		$get = true;
		$str_exchange = '';
		for($i=0;$i<strlen($content_split[1]);$i++){
			if ($get && $content_split[1][$i] !=' ' && $content_split[1][$i] !='(' && $content_split[1][$i] !=')'){
				$str_exchange .= $content_split[1][$i];
			}
			if ($content_split[1][$i] =='(') $get = false;
			if ($content_split[1][$i] ==')') $get = true;
		}
		// Exchange
		$exchanges = explode(';', $str_exchange);
		foreach ($exchanges as $exchange){
			if(strpos($exchange,$find_bet) !== false){
				$return['exchange'] = array(
						'home'=>substr($exchange, -12, 4),
						'draw'=>substr($exchange, -8, 4),
						'away'=>substr($exchange, -4, 4)
				);
				break;
			}
		}
		$return['exchange'] = $exchanges;
		return $return;
	
	}
	/*
	 * Get Over/Under bet rate
	 * 
	 *Array
	 *	(
	 *	    [rate] => +0.5
	 *	    [home] => 1.57
	 *	    [away] => 2.35
	 *	) 
	 *
	 * */
	function getBetOverUnder($find_bet='bet365'){
		if (empty($this->datas)) return array();
		$return = array();
		$datas = $this->datas;
		//	preg_match_all("/<div id=\"block-1x2-ft\" style=\"display: block;\">(.*)<div id=\"block-1x2-1hf\" style=\"display: none;\">/U", $datas, $out);
		preg_match_all("/<div id=\"block-under-over-ft\" style=\"display: block;\">(.*)<div id=\"block-under-over-1hf\" style=\"display: none;\">/U", $datas, $out);
		$content = isset($out[1][0])?strip_tags($out[1][0],'<table><tr><td>'):'';
		//	echo $content;
		$content = str_replace('</table>', '--', $content);
		$content = str_replace('</td>', ':', $content);
		$content = strip_tags($content,'<tr>');
		$content = str_replace('&nbsp;', '', $content);
		$content = str_replace('<tr>', '', $content);
		$content = str_replace('<tr class="odd">', '', $content);
		$content = str_replace('<tr class="even">', '', $content);
		$content = str_replace('</tr>', ';', $content);
		$contents_split = explode('--', $content);
		//processs
		foreach ($contents_split as $ct_split) {
			$splits = explode(';', $ct_split);
			foreach ($splits as $split){
				if(strpos($split,$find_bet) !== false){
					//bet365:0.5:1.06:10.00:
					$OverUnder = explode(':', $split);
					$return[] = array(
							'total'=>$this->cleanSign($OverUnder[1]),
							'over'=>$this->cleanSign($OverUnder[2]),
							'under'=>$this->cleanSign($OverUnder[3])
					);
					break;
				}
			}
		}
		if($return){
			$tb = count($return)/2;
			return $return[$tb];
		}else{
			return array(
					'total'=>0,
					'over'=>0,
					'under'=>0
			);
		}
	
	}
	/*
	 * Get Over/Under bet rate
	 * Array
	 *	(
	 *	    [total] => 3.5
	 *	    [over] => 3.40
	 *	    [under] => 1.30
	 *	)
	 *
	 * */
function getBetHandicap($find_bet='bet365'){
	if (empty($this->datas)) {
		return array();
	}else{
		//$this->log->addlog($this->datas);
	}
	$return = array();
	//	preg_match_all("/<div id=\"block-under-over-ft\" style=\"display: block;\">(.*)<div id=\"block-under-over-1hf\" style=\"display: none;\">/U", $datas, $out);
	preg_match_all("/<div id=\"block-asian-handicap-ft\" style=\"display: block;\">(.*)<div id=\"block-asian-handicap-1hf\" style=\"display: none;\">/U", $this->datas, $out);
// 	$content = strip_tags($out[1][0],'<table><tr><td>');
	$content = isset($out[1][0])?strip_tags($out[1][0],'<table><tr><td>'):'';
	//	echo $content;
	$content = str_replace('</table>', '--', $content);
	$content = str_replace('</td>', ':', $content);
	$content = strip_tags($content,'<tr>');
	$content = str_replace('&nbsp;', '', $content);
	$content = str_replace('<tr>', '', $content);
	$content = str_replace('<tr class="odd">', '', $content);
	$content = str_replace('<tr class="even">', '', $content);
	$content = str_replace('</tr>', ';', $content);
	$contents_split = explode('--', $content);
	//processs
// 	echo "<pre>";
// 	print_r($contents_split);
	foreach ($contents_split as $ct_split) {
		$splits = explode(';', $ct_split);
		foreach ($splits as $split){
			if(strpos($split,$find_bet) !== false){
				//bet365:0, -0.5:2.50:1.50:
				//				echo $split.'->';
				$handicap = explode(':', $split);
				$return[] = array(
						'rate'=>$this->hadicapRate($handicap[1]),
						'home'=>$this->cleanSign($handicap[2]),
						'away'=>$this->cleanSign($handicap[3])
						);
							break;
						}
						}
	}
	
	if($return){
		$tb = count($return)/2;
		return $return[$tb];
	}else{
		return array(
				'rate'=>0,
				'home'=>0,
				'away'=>0
			);
	}
	}// end funtion
	/*
	 * clean sign
	 * */
	function cleanSign($string){
		$string = str_replace(array('+','-'), '', $string);
		return $string;
	}
	/*
	 * clean sign
	 * */
	function realRate($rate, $commistion){
		return empty($rate)?0:$rate-$commistion;
	}
	/*
	 * clean sign
	* */
	function hadicapRate($str){
		$str =  trim($str);
		if(strpos($str,',')){
			$str_split = explode(',', $str);
			//print_r($str_split);
			$return = $str_split[0]-$str_split[1];
		}else{
			$return = $str;
		}
		return $return;
	}
}